const npath = require('path');
// const MiniFixPlugin = require('@antmjs/plugin-mini-fix');
const webpack = require('webpack');
const globalProgressPlugin = require('./globalProgressPlugin');

module.exports = function (chain) {
  //在开发调试 pnpm link组件库时 vantui组件调试出现的问题
  if (process.env.TARO_ENV === 'weapp' && process.env.NODE_ENV === 'development') {
    chain.plugin('globalProgressPlugin').use(new webpack.ProgressPlugin(globalProgressPlugin));
  }

  // // 解决支付宝小程序和钉钉小程序报Object is not a function的问题
  // chain.module
  //   .rule('global-loader')
  //   .test(/node_modules[\\/]webpack[\\/]buildin[\\/]global\.js/i)
  //   .pre()
  //   .use('global-loader')
  //   .loader(npath.join(process.cwd(), 'config/webpack/globalLoader'));

  // vantui 等其他问题
  chain.module
    .rule('vantui-loader')
    .test(/node_modules[\\/]@antmjs[\\/]vantui[\\/]/i)
    .pre()
    .use('vantui-loader')
    .loader(npath.join(process.cwd(), 'config/webpack/vantuiLoader'));

  // 开发模式下的打包太大了，自定义一下减少包大小
  if (process.env.NODE_ENV === 'development') {
    chain.merge({
      optimization: {
        minimize: true,
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            'custom-components': {
              test: /[\\/]src[\\/](components).*[\\/]/,
              name: 'custom-components',
              enforce: true,
              priority: 26,
            },
            'custom-common': {
              test: /[\\/]node_modules[\\/](dayjs|lodash).*[\\/]/,
              name: 'custom-common',
              enforce: true,
              priority: 25,
            },
            'vh-mobile': {
              test: /[\\/]node_modules[\\/](@vh-mobile|vh-mobile).*[\\/]/,
              name: 'vh-mobile',
              enforce: true,
              priority: 24,
            },
            vantui: {
              test: /[\\/]node_modules[\\/](@antmjs|antmjs).*[\\/]/,
              name: 'vantui',
              enforce: true,
              priority: 23,
            },
            vant: {
              test: /[\\/]node_modules[\\/](@vant|vant).*[\\/]/,
              name: 'vant',
              enforce: true,
              priority: 22,
            },
            recoil: {
              test: /[\\/]node_modules[\\/](recoil).*[\\/]/,
              name: 'recoil',
              enforce: true,
              priority: 21,
            },
            'react-vendor': {
              test: /[\\/]node_modules[\\/](react).*[\\/]/,
              name: 'react-vendor',
              enforce: true,
              priority: 20,
            },
            // 'vendors-modules': {
            //   test: /[\\/]node_modules[\\/]/,
            //   name: 'vendors-modules',
            //   enforce: true,
            //   priority: 1,
            // },
          },
        },
      },
    });
  }
};
